

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.ico">
  <link rel="icon" href="/img/favicon.ico">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="叶尘的技术升级小木屋">
  <meta name="author" content="叶尘">
  <meta name="keywords" content="">
  
  <title>3、数据类型 - 叶尘的技术升级小木屋</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->

  
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/shubiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/gundongtiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/daziyanse.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/toubudaziji.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.yechen-blog.cn","root":"/","version":"1.8.11","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>YECHEN</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-qrcode"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/playlist/">
                <i class="iconfont icon-music"></i>
                音乐
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-books"></i>
                链接
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/">
                    
                    主题博客
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/guide/">
                    
                    配置指南
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/icon/">
                    
                    图标用法
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://www.bilibili.com">
                    
                    哔哩哔哩
                  </a>
                
              </div>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/banner.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="3、数据类型">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      叶尘
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-09-13 22:00" pubdate>
        星期一, 九月 13日 2021, 10:00 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.9k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      23
       分钟
    </span>
  

  
  
    
      <!-- LeanCloud 统计文章PV -->
      <span id="leancloud-page-views-container" class="post-meta" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="leancloud-page-views"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">3、数据类型</h1>
            
            <div class="markdown-body">
              <p>八个 ==基本数据类型== 及其占用空间大小</p>
<table>
<thead>
<tr>
<th>类型</th>
<th>存储空间</th>
<th>范围</th>
</tr>
</thead>
<tbody><tr>
<td>byte</td>
<td>1字节</td>
<td>-128 ~ 127</td>
</tr>
<tr>
<td>short</td>
<td>2字节</td>
<td>-(-2^15^) ~ (2^15^-1)</td>
</tr>
<tr>
<td>int</td>
<td>4字节</td>
<td>-(2^31^) ~ (2^31^-1)</td>
</tr>
<tr>
<td>long</td>
<td>8字节</td>
<td>-(2^63^) ~ (2^63^-1)</td>
</tr>
<tr>
<td>float</td>
<td>4字节</td>
<td></td>
</tr>
<tr>
<td>double</td>
<td>8字节</td>
<td></td>
</tr>
<tr>
<td>boolean</td>
<td>1字节</td>
<td></td>
</tr>
<tr>
<td>char</td>
<td>2字节</td>
<td>0 ~ （2^16^ - 1）</td>
</tr>
</tbody></table>
<h4 id="3-1-整数型字面量四种表现形式："><a href="#3-1-整数型字面量四种表现形式：" class="headerlink" title="3.1 整数型字面量四种表现形式："></a>3.1 整数型字面量四种表现形式：</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 十进制</span><br><span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>; <br>System.out.println(a); <span class="hljs-comment">// 10</span><br><br><span class="hljs-comment">// 八进制</span><br><span class="hljs-keyword">int</span> b = <span class="hljs-number">010</span>;<br>System.out.println(b); <span class="hljs-comment">// 8</span><br><br><span class="hljs-comment">// 十六进制</span><br><span class="hljs-keyword">int</span> c = <span class="hljs-number">0x10</span>;<br>System.out.println(c); <span class="hljs-comment">// 16</span><br><br><span class="hljs-keyword">int</span> x = <span class="hljs-number">16</span>; <span class="hljs-comment">//十进制方式</span><br>System.out.println(x);<span class="hljs-comment">// 16</span><br><br><span class="hljs-comment">// 二进制（JDK8的新特性，低版本不支持。）</span><br><span class="hljs-keyword">int</span> d = <span class="hljs-number">0b10</span>;<br>System.out.println(d); <span class="hljs-comment">// 2</span><br></code></pre></td></tr></table></figure>



<h4 id="3-2-数据类型转换："><a href="#3-2-数据类型转换：" class="headerlink" title="3.2 数据类型转换："></a>3.2 数据类型转换：</h4><ul>
<li><p>在 java 中有一条非常重要的结论，必须记住：==在任何情况下，整数型的“字面量/数据”默认被当做 int 类型处理==。（记住就行）<br>如果希望该“整数型字面量”被当做 long 类型来处理，需要在“字面量”后面添加 L / l 建议使用大写 L，因为小写 l 和 1 傻傻分不清。</p>
</li>
<li><p>小容量可以直接赋值给大容量，称为<strong>自动类型转换</strong>。</p>
</li>
<li><p>大容量不能直接赋值给小容量，<strong>需要使用强制类型转换符进行强转</strong>。但需要注意的是：加强制类型转换符之后，虽然编译通过了，但是运行的时候可能会损失精度。</p>
</li>
<li><p><strong>但是：java 中有一个语法规则：当这个整数型字面量没有超出 byte（或 short）的取值范围，那么这个整数型字面量可以直接赋值给 byte（或 short）类型的变量。</strong></p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IntTest04</span> </span>&#123;<br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>		<span class="hljs-comment">// 分析：以下代码编译可以通过吗？</span><br>		<span class="hljs-comment">// 300 被默认当做int类型</span><br>		<span class="hljs-comment">// b变量是byte类型</span><br>		<span class="hljs-comment">// 大容量转换成小容量，要想编译通过，必须使用强制类型转换符</span><br>		<span class="hljs-comment">// 错误: 不兼容的类型: 从int转换到byte可能会有损失</span><br>		<span class="hljs-keyword">byte</span> b = <span class="hljs-number">300</span>;<br><br>		<span class="hljs-comment">// 要想让以上的程序编译通过，必须加强制类型转换符</span><br>		<span class="hljs-comment">// 虽然编译通过了，但是可能精度损失。</span><br>		<span class="hljs-comment">// 300这个int类型对应的二进制：00000000 00000000 00000001 00101100</span><br>		<span class="hljs-comment">// byte占用1个字节，砍掉前3个字节，结果是：00101100 (44)</span><br>		<span class="hljs-keyword">byte</span> b = (<span class="hljs-keyword">byte</span>)<span class="hljs-number">300</span>;<br>		System.out.println(b); <span class="hljs-comment">// 44</span><br>        <br>        <span class="hljs-comment">// 这样是都不会报错的</span><br>        <span class="hljs-keyword">byte</span> x = <span class="hljs-number">1</span>;<br>		<span class="hljs-keyword">byte</span> y = <span class="hljs-number">127</span>;<br>        <span class="hljs-keyword">short</span> s = <span class="hljs-number">1</span>;<br>		<span class="hljs-keyword">short</span> s1 = <span class="hljs-number">32767</span>;<br>        <br>        <span class="hljs-comment">// 但这样就会报错了</span><br>        <span class="hljs-keyword">byte</span> x = <span class="hljs-number">128</span>;<br>        <span class="hljs-keyword">short</span> x = <span class="hljs-number">32768</span>;<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>byte、char、short 做混合运算的时候，各自先转换成 int 再做运算</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IntTest06</span></span>&#123;<br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>&#123;<br><br>		<span class="hljs-keyword">char</span> c1 = <span class="hljs-string">&#x27;a&#x27;</span>;<br>		<span class="hljs-keyword">byte</span> b = <span class="hljs-number">1</span>;<br><br>		<span class="hljs-comment">// 注意：这里的&quot;+&quot;是负责求和的</span><br>		System.out.println(c1 + b); <span class="hljs-comment">// 98</span><br><br>		<span class="hljs-comment">// 错误: 不兼容的类型: 从int转换到short可能会有损失</span><br>		<span class="hljs-comment">//short s = c1 + b; // 编译器不知道这个加法最后的结果是多少。只知道是int类型。</span><br><br>		<span class="hljs-comment">// 这样修改行吗？</span><br>		<span class="hljs-comment">// 错误: 不兼容的类型: 从int转换到short可能会有损失</span><br>		<span class="hljs-comment">// short s = (short)c1 + b;</span><br><br>		<span class="hljs-keyword">short</span> s = (<span class="hljs-keyword">short</span>)(c1 + b);<br><br>		<span class="hljs-comment">// short k = 98;</span><br><br>		<span class="hljs-keyword">int</span> a = <span class="hljs-number">1</span>;<br>		<span class="hljs-comment">// 错误: 不兼容的类型: 从int转换到short可能会有损失</span><br>		<span class="hljs-comment">// short x = 1; 可以</span><br>		<span class="hljs-keyword">short</span> x = a; <span class="hljs-comment">// 不可以，编译器只知道a是int类型，不知道a中存储的是哪个值。</span><br>		System.out.println(x);<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>多种数据类型做混合运算的时候，最终的结果类型是“最大容量”对应的类型。(char+short+byte 这个除外，原因如上)</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">long</span> a = <span class="hljs-number">10L</span>;<br><span class="hljs-keyword">char</span> c = <span class="hljs-string">&#x27;a&#x27;</span>;<br><span class="hljs-keyword">short</span> s = <span class="hljs-number">100</span>;<br><span class="hljs-keyword">int</span> i = <span class="hljs-number">30</span>;<br><br><span class="hljs-keyword">long</span> sum = a + c + s + i;<br><br><span class="hljs-comment">// 会出现：错误: 不兼容的类型: 从long转换到int可能会有损失</span><br><span class="hljs-comment">// int sum = a + c + s + i;</span><br><br><span class="hljs-comment">// 求和</span><br>System.out.println(sum); <span class="hljs-comment">//237</span><br></code></pre></td></tr></table></figure>

<ul>
<li>练习</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TypeTransferTest</span></span>&#123;<br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>&#123;<br>		<span class="hljs-comment">// 编译报错，因为1000已经超出范围了。</span><br>        <span class="hljs-comment">// 错误: 不兼容的类型: 从int转换到byte可能会有损失</span><br>		<span class="hljs-keyword">byte</span> b1 = <span class="hljs-number">1000</span>;<br>		<span class="hljs-comment">// 可以</span><br>		<span class="hljs-keyword">byte</span> b2 = <span class="hljs-number">20</span>;<br>		<span class="hljs-comment">// 可以</span><br>		<span class="hljs-keyword">short</span> s = <span class="hljs-number">1000</span>;<br>		<span class="hljs-comment">// 可以</span><br>		<span class="hljs-keyword">int</span> c = <span class="hljs-number">1000</span>;<br>		<span class="hljs-comment">// 可以</span><br>		<span class="hljs-keyword">long</span> d = c;<br>		<span class="hljs-comment">// 编译报错</span><br>        <span class="hljs-comment">// 错误: 不兼容的类型: 从long转换到int可能会有损失</span><br>		<span class="hljs-keyword">int</span> e = d;<br>		<span class="hljs-comment">// 可以</span><br>		<span class="hljs-keyword">int</span> f = <span class="hljs-number">10</span> / <span class="hljs-number">3</span>;<br>		<span class="hljs-comment">// 可以</span><br>		<span class="hljs-keyword">long</span> g = <span class="hljs-number">10</span>;<br>		<span class="hljs-comment">// 编译报错</span><br>        <span class="hljs-comment">// 错误: 不兼容的类型: 从long转换到int可能会有损失</span><br>		<span class="hljs-keyword">int</span> h = g / <span class="hljs-number">3</span>;<br>		<span class="hljs-comment">// 可以</span><br>		<span class="hljs-keyword">long</span> m = g / <span class="hljs-number">3</span>;<br>		<span class="hljs-comment">// 编译报错</span><br>        <span class="hljs-comment">// 错误: 不兼容的类型: 从int转换到byte可能会有损失</span><br>		<span class="hljs-keyword">byte</span> x = (<span class="hljs-keyword">byte</span>)g / <span class="hljs-number">3</span>;<br>		<span class="hljs-comment">// 可以</span><br>		<span class="hljs-keyword">short</span> y = (<span class="hljs-keyword">short</span>)(g / <span class="hljs-number">3</span>);<br>		<span class="hljs-comment">// 可以</span><br>		<span class="hljs-keyword">short</span> i = <span class="hljs-number">10</span>;<br>		<span class="hljs-comment">// 可以</span><br>		<span class="hljs-keyword">byte</span> j = <span class="hljs-number">5</span>;<br>		<span class="hljs-comment">// 编译报错</span><br>        <span class="hljs-comment">// 错误: 不兼容的类型: 从int转换到short可能会有损失</span><br>		<span class="hljs-keyword">short</span> k = i + j;<br>		<span class="hljs-comment">// 可以</span><br>		<span class="hljs-keyword">int</span> n = i + j;<br>		<span class="hljs-comment">// 可以</span><br>		<span class="hljs-keyword">char</span> cc = <span class="hljs-string">&#x27;a&#x27;</span>;<br>		System.out.println(cc); <span class="hljs-comment">// a</span><br>		System.out.println((<span class="hljs-keyword">byte</span>)cc); <span class="hljs-comment">// 97</span><br>		<span class="hljs-comment">// cc 会先自动转换成int类型，再做运算</span><br>		<span class="hljs-keyword">int</span> o = cc + <span class="hljs-number">100</span>;<br>		System.out.println(o); <span class="hljs-comment">// 197</span><br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h4 id="3-3-原码-反码-补码"><a href="#3-3-原码-反码-补码" class="headerlink" title="3.3 原码 反码 补码"></a>3.3 原码 反码 补码</h4><ul>
<li>计算机在底层存储数据的时候，一律存储的是“==<strong>二进制的补码形式</strong>==”</li>
<li><strong>对于一个正数来说：二进制原码、反码、补码是同一个，完全相同</strong></li>
<li>对于一个负数来说：二进制原码、反码、补码是什么关系：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">byte</span> i = -<span class="hljs-number">1</span>;<br><span class="hljs-comment">// 对应的二进制原码：10000001</span><br><span class="hljs-comment">// 对应的二进制反码（符号位不变，其它位取反）：11111110</span><br><span class="hljs-comment">// 对应的二进制补码（反码+1）：11111111</span><br></code></pre></td></tr></table></figure>

<h4 id="3-4-关于java语言中的浮点型数据"><a href="#3-4-关于java语言中的浮点型数据" class="headerlink" title="3.4 关于java语言中的浮点型数据"></a>3.4 关于java语言中的浮点型数据</h4><p><strong>float是单精度</strong><br><strong>double是双精度</strong></p>
<ul>
<li>float 和 double 存储数据的时候都是存储的近似值</li>
<li>注意：任意一个浮点型都比整数型空间大。float 容量 &gt; long 容量。</li>
<li><strong>java 中规定，任何一个浮点型数据默认被当做 double 来处理，如果想让这个浮点型字面量被当做 float 类型来处理，那么请在字面量后面添加 F / f 。</strong>    </li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 错误</span><br><span class="hljs-keyword">float</span> a = <span class="hljs-number">3.14</span>;<br><span class="hljs-comment">// 正确</span><br><span class="hljs-keyword">float</span> a = <span class="hljs-number">3.14f</span>;<br><span class="hljs-comment">// 正确</span><br><span class="hljs-keyword">double</span> a = <span class="hljs-number">3.14</span>;<br></code></pre></td></tr></table></figure>

<h4 id="3-5-关于字符型：char"><a href="#3-5-关于字符型：char" class="headerlink" title="3.5 关于字符型：char"></a>3.5 关于字符型：char</h4><ul>
<li><p>char占用2个字节。</p>
</li>
<li><p>char的取值范围：[0-65535]</p>
</li>
<li><p>char采用 <strong>unicode</strong> 编码方式。</p>
</li>
<li><p>char类型的字面量使用单引号括起来。</p>
</li>
<li><p>char可以存储一个汉字。</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">char</span> c1 = <span class="hljs-string">&#x27;中&#x27;</span>;<br>System.out.println(c1);<br><br><span class="hljs-keyword">char</span> c2 = <span class="hljs-string">&#x27;a&#x27;</span>;<br>System.out.println(c2);<br><br><span class="hljs-comment">// 0如果加上单引号的话，0就不是数字0了，就是文字0，它是1个字符。</span><br><span class="hljs-keyword">char</span> c3 = <span class="hljs-string">&#x27;0&#x27;</span>;<br>System.out.println(c3);<br><br><span class="hljs-comment">// 以下就会出现错误</span><br><span class="hljs-comment">// 错误: 不兼容的类型: String无法转换为char</span><br><span class="hljs-keyword">char</span> c4 = <span class="hljs-string">&quot;a&quot;</span>;<br><br><span class="hljs-comment">// 错误: 未结束的字符文字</span><br><span class="hljs-keyword">char</span> c5 = <span class="hljs-string">&#x27;ab&#x27;</span>;<br><br><span class="hljs-comment">// 错误: 未结束的字符文字</span><br><span class="hljs-keyword">char</span> c6 = <span class="hljs-string">&#x27;1.08&#x27;</span>;<br></code></pre></td></tr></table></figure>

<ul>
<li>当一个整数赋值给 char 类型变量的时候，会自动转换成 char 字符型，最终的结果是一个字符</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">char</span> a = <span class="hljs-number">97</span>;<br>System.out.println(a); <span class="hljs-comment">// a</span><br></code></pre></td></tr></table></figure>

<ul>
<li>当一个整数没有超出 byte  short  char 的取值范围的时候，这个整数可以直接赋值给 byte  short  char类型的变量。</li>
</ul>
<h4 id="3-6-关于java中的转义字符"><a href="#3-6-关于java中的转义字符" class="headerlink" title="3.6 关于java中的转义字符"></a>3.6 关于java中的转义字符</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// \t : 一个制表位</span><br>System.out.println(<span class="hljs-string">&quot;北京\t天津\t上海&quot;</span>);<br><span class="hljs-comment">// \n : 换行符</span><br>System.out.println(<span class="hljs-string">&quot;jack\nsmith\nmary&quot;</span>);<br><span class="hljs-comment">// \\ : 一个 \</span><br>System.out.println(<span class="hljs-string">&quot;C:\\\\Windows\\\\System32\\\\cmd.exe&quot;</span>);<span class="hljs-comment">// 输出 //</span><br><span class="hljs-comment">// \&quot; : 一个 &quot;</span><br>System.out.println(<span class="hljs-string">&quot;老韩说：\&quot;要好好学习 Java\&quot;&quot;</span>);<br><span class="hljs-comment">// \r : 一个回车</span><br>System.out.println(<span class="hljs-string">&quot;韩顺平教育\r北京&quot;</span>);<br></code></pre></td></tr></table></figure>



<h4 id="3-7-关于-boolean-类型"><a href="#3-7-关于-boolean-类型" class="headerlink" title="3.7 关于 boolean 类型"></a>3.7 关于 boolean 类型</h4><ul>
<li>在 java 语言中 boolean 类型只有两个值，没有其他值：true 和 false。不像 C 或者 C++，C 语言中 1 和 0 也可以表示布尔类型。</li>
<li>boolean 类型在实际开发中使用在<strong>逻辑判断当中</strong>，通常放到<strong>条件的位置上</strong>（充当条件）</li>
</ul>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/Java-%E5%9F%BA%E7%A1%80/">Java 基础</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Java-%E5%9F%BA%E7%A1%80/">Java 基础</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/09/13/Java%E5%9F%BA%E7%A1%80-2021-09-13-%E8%BF%90%E7%AE%97%E7%AC%A6/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">4、运算符</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/09/13/Java%E5%9F%BA%E7%A1%80-2021-09-13-%E5%8F%98%E9%87%8F/">
                        <span class="hidden-mobile">2、变量</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.3/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.1/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>



  <script  src="/js/local-search.js" ></script>






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>














  
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxuehua.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/caidai.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/love.js"></script>
<script src="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script>



<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>

  <!--动态线条背景-->
  <script type="text/javascript" color="70,119,184" opacity='1' zIndex="-2" count="200" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/koharu.model.json"},"display":{"position":"left","width":200,"height":400},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
